/**
 * 
 */
package com.zznode.client.wlanpolling.main;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.service.Wifi;
import com.zznode.client.wlanpolling.utils.NetworkUtils;
import com.zznode.client.wlanpolling.widget.SignalView;

/**
 * @author Bean
 *
 */
public class APListAdaptor extends BaseExpandableListAdapter {
	/**
	 * 接入点名称(ssid)
	 */
	public static final String AP_NAME = "apname";
	
	/**
	 * bssid
	 */
	public static final String AP_BSSID = "bssid";
	
	/**
	 * AP当前状态
	 */
	public static final String AP_STATUS = "apstatus";
	/**
	 * 是否测试
	 */
	public static final String AP_TEST = "aptest";
	/**
	 * Mac地址
	 */
	public static final String MAC = "mac";
	/**
	 * 认证授权方式
	 */
	public static final String AUTHENTICATION = "authentication";
	/**
	 * 信道
	 */
	public static final String CHANNEL = "channel";
	/**
	 * 场强
	 */
	public static final String dBm = "dBm";
	
	private Context context;
	private List<String> titles =new ArrayList<String>();
	List<List<Wifi>> children = new ArrayList<List<Wifi>>();
	
	public APListAdaptor(Context context,List<String> titles,List<List<Wifi>> children) {
		this.context = context;
		this.titles = titles;
		this.children = children;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		View view = convertView;
		
        if(view == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.ap_listchildview, null);
        }
        
        Wifi wifi = children.get(groupPosition).get(childPosition);
        
        //apn name
        final TextView tvApnName = (TextView) view.findViewById(R.id.txt_apname);
        if(wifi.getSSID().length() > 10) {
        	tvApnName.setText(wifi.getSSID().substring(0, 9) + "...");
        } else {
        	tvApnName.setText(wifi.getSSID());
        }
        
        //测试状态
        final TextView tvTest = (TextView) view.findViewById(R.id.txt_test);
        if(wifi.isTested()) {
        	tvTest.setText("已测试");
        	tvTest.setTextColor(Color.GREEN);
        }else{
        	tvTest.setTextColor(Color.WHITE);
        	tvTest.setText("未测试");
        }
        
        final TextView tvCommited = (TextView) view.findViewById(R.id.txt_commit);
        if(wifi.isCommited()) {
        	tvCommited.setText("已提交");
        } else {
        	tvCommited.setText("未提交");
        }
        
        //状态
        final ImageView imageStatus = (ImageView) view.findViewById(R.id.img_status);
        if(wifi.isOpened()) {
        	imageStatus.setImageResource(R.drawable.ap_on);
        } else {
        	imageStatus.setVisibility(View.GONE);
        }
        
        //Mac地址
        final TextView tvMacAddress = (TextView)view.findViewById(R.id.txt_mac);
        tvMacAddress.setText(wifi.getBSSID());
        
        //认证方式
        final TextView tvAuthentic = (TextView) view.findViewById(R.id.txt_authentication);
        tvAuthentic.setText(wifi.getCapabilities());
        
        //通道
        final TextView tvChannel = (TextView) view.findViewById(R.id.txt_channel);
        tvChannel.setText(NetworkUtils.getChannel(wifi.getFrequency()) + "");
        
        //信号强度
        final TextView tvSignal = (TextView) view.findViewById(R.id.txt_signal);
        tvSignal.setText(wifi.getLevel() + "");
        
        //信号强度的图片
        final SignalView signalView = (SignalView)view.findViewById(R.id.img_sigal);
        signalView.setSignal(wifi.getLevel());
        
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return children.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return titles.get(groupPosition).toString();
	}

	@Override
	public int getGroupCount() {
		return titles.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
        View view = convertView;
        //将原来的样式替换新样式
        if(view == null){
            LayoutInflater inflater_new = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater_new.inflate( R.layout.ap_listview , null);
        }
        
        TextView text_titel = (TextView) view.findViewById(R.id.tv_fristtitel);
        text_titel.setText(getGroup(groupPosition).toString());
        
        ImageView groupIndicator = (ImageView) view.findViewById(R.id.group_indicator);
        if(!isExpanded) {
        	groupIndicator.setImageResource(R.drawable.icon_close);
        } else {
        	groupIndicator.setImageResource(R.drawable.icon_open);
        }
        
        return view;

	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
